/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entity;

import javax.swing.JFrame;

/**
 *
 * @author 
 */
public class ManipulationFrame extends Thread
{
    private JFrame frame;
    private int width;
    private int height;
    private boolean open;

    public ManipulationFrame(JFrame frame, int width, int height, boolean open)
    {
        this.frame = frame;
        this.width = width;
        this.height = height;
        this.open = open;
    }

    public ManipulationFrame(JFrame frame)
    {
        this.frame = frame;
        this.open = false;
    }
    
    public void setSizeLocation(int w, int h, int x, int y, int sleep) throws Exception
    {
        try
        {
            frame.setSize(w, h);
            frame.setLocation(x, y);
            Thread.sleep(sleep);
        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

    public void open() throws Exception
    {
        try
        {
            
            int w = frame.getSize().width;
            int h = frame.getSize().height;
            int wh = 10;
            int x = frame.getLocation().x;
            int y = frame.getLocation().y;

            while(width > w)
            {
                w += wh;
                x -= wh / 2;
                setSizeLocation(w, h, x, y, 5);
            }
            
            while(height > h)
            {
                h += wh;
                y -= wh / 2;
                setSizeLocation(w, h, x, y, 10);
            }           
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public void close() throws Exception
    {
        try
        {
            width = frame.getSize().width;
            height = frame.getSize().height;
            int wh = 10;
            int x = frame.getLocation().x;
            int y = frame.getLocation().y;
            
            while(height > 25)
            {
                height -= wh;
                y += wh / 2;
                setSizeLocation(width, height, x, y, 10);
            }
            
            while(width > 0)
            {
                width -= wh;
                x += wh / 2;
                setSizeLocation(width, height, x, y, 5);
            }
            
            System.exit(0);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    @Override
    public void run()
    {
        try
        {
            if(open)
            {
                open();
            }
            else
            {
                close();
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }
    }
}
